﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BasicData.Clients.Dto;
using WMS.BasicData.Clients;
using WMS.StorageManagement.RepositoryDefinitions.Dto;
using WMS.Pagination;

namespace WMS.StorageManagement.RepositoryDefinitions
{
    /// <summary>
    /// 仓库定义服务
    /// </summary>
    public class RepositoryDefinitionAppService: ApplicationService, IRepositoryDefinitionAppService
    {
        /// <summary>
        /// 仓库定义仓储接口
        /// </summary>
        private readonly IRepository<RepositoryDefinition> _repositoryDefinitionAppService;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repositoryDefinitionAppService">仓库定义仓储接口</param>
        /// <param name="mapper">映射</param>
        public RepositoryDefinitionAppService(IRepository<RepositoryDefinition> repositoryDefinitionAppService, IMapper mapper)
        {
            _repositoryDefinitionAppService = repositoryDefinitionAppService;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加仓库定义信息
        /// </summary>
        /// <param name="repositoryDefinitionDto">仓库定义Dto</param>
        /// <returns>返回结果</returns>
        public async Task<int> Insert(RepositoryDefinitionDto repositoryDefinitionDto)
        {
            RepositoryDefinition repositoryDefinition = _mapper.Map<RepositoryDefinition>(repositoryDefinitionDto);
            await _repositoryDefinitionAppService.InsertAsync(repositoryDefinition);
            return 1;
        }

        /// <summary>
        /// 显示仓库定义信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="repositoryName">仓库名称</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> GetInquire(int pageIndex, int pageSize, string? repositoryName)
        {
            var repositoryDefinition = await _repositoryDefinitionAppService.GetListAsync(m => string.IsNullOrEmpty(repositoryName) || m.RepositoryName!.Contains(repositoryName));
            PageResult pageResult = new PageResult();
            pageResult.RowCount = repositoryDefinition.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = repositoryDefinition.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }

        /// <summary>
        /// 删除仓库定义信息
        /// </summary>
        /// <param name="ids">仓库定义编号</param>
        /// <returns>返回结果</returns>
        public async Task<int> Deleted(string ids)
        {
            var Id = ids.Split(',');
            var reslut = 0;
            foreach (var id in Id)
            {
                await _repositoryDefinitionAppService.DeleteAsync(m => m.Id == Convert.ToInt32(id));
                reslut++;
            }
            return reslut > 0 ? 1 : 0;
        }

        /// <summary>
        /// 编辑客户信息
        /// </summary>
        /// <param name="repositoryDefinitionDto">仓库定义Dto</param>
        /// <returns>返回结果</returns>
        public async Task<int> Update(RepositoryDefinitionDto repositoryDefinitionDto)
        {
            RepositoryDefinition repositoryDefinition = _mapper.Map<RepositoryDefinition>(repositoryDefinitionDto);
            await _repositoryDefinitionAppService.UpdateAsync(repositoryDefinition);
            return 1;
        }
    }
}
